﻿using System;
using UnityEngine;

namespace RootMotion.FinalIK
{
	// Token: 0x02000015 RID: 21
	[Serializable]
	public class Finger
	{
		// Token: 0x1700000C RID: 12
		// (get) Token: 0x0600014F RID: 335 RVA: 0x00002FBC File Offset: 0x000011BC
		// (set) Token: 0x06000150 RID: 336 RVA: 0x00002FC4 File Offset: 0x000011C4
		public bool initiated { get; set; }

		// Token: 0x1700000D RID: 13
		// (get) Token: 0x06000151 RID: 337 RVA: 0x00002FCD File Offset: 0x000011CD
		// (set) Token: 0x06000152 RID: 338 RVA: 0x00002FDA File Offset: 0x000011DA
		public Vector3 IKPosition
		{
			get
			{
				return this.solver.IKPosition;
			}
			set
			{
				this.solver.IKPosition = value;
			}
		}

		// Token: 0x1700000E RID: 14
		// (get) Token: 0x06000153 RID: 339 RVA: 0x00002FE8 File Offset: 0x000011E8
		// (set) Token: 0x06000154 RID: 340 RVA: 0x00002FF5 File Offset: 0x000011F5
		public Quaternion IKRotation
		{
			get
			{
				return this.solver.IKRotation;
			}
			set
			{
				this.solver.IKRotation = value;
			}
		}

		// Token: 0x06000155 RID: 341 RVA: 0x00003003 File Offset: 0x00001203
		public bool IsValid(ref string errorMessage)
		{
			if (this.bone1 == null || this.bone2 == null || this.tip == null)
			{
				errorMessage = "One of the bones in the Finger Rig is null, can not initiate solvers.";
				return false;
			}
			return true;
		}

		// Token: 0x06000156 RID: 342 RVA: 0x0001ED24 File Offset: 0x0001CF24
		public void Initiate(Transform hand, int index)
		{
			this.initiated = false;
			string empty = string.Empty;
			if (!this.IsValid(ref empty))
			{
				Warning.Log(empty, hand, false);
				return;
			}
			this.solver = new IKSolverLimb();
			this.solver.IKPositionWeight = this.weight;
			this.solver.bendModifier = IKSolverLimb.BendModifier.Target;
			this.solver.bendModifierWeight = 1f;
			this.IKPosition = this.tip.position;
			this.IKRotation = this.tip.rotation;
			if (this.bone3 != null)
			{
				this.bone3RelativeToTarget = Quaternion.Inverse(this.IKRotation) * this.bone3.rotation;
				this.bone3DefaultLocalPosition = this.bone3.localPosition;
				this.bone3DefaultLocalRotation = this.bone3.localRotation;
			}
			this.solver.SetChain(this.bone1, this.bone2, this.tip, hand);
			this.solver.Initiate(hand);
			this.initiated = true;
		}

		// Token: 0x06000157 RID: 343 RVA: 0x0001EE34 File Offset: 0x0001D034
		public void FixTransforms()
		{
			if (!this.initiated)
			{
				return;
			}
			this.solver.FixTransforms();
			if (this.bone3 != null)
			{
				this.bone3.localPosition = this.bone3DefaultLocalPosition;
				this.bone3.localRotation = this.bone3DefaultLocalRotation;
			}
		}

		// Token: 0x06000158 RID: 344 RVA: 0x0001EE8C File Offset: 0x0001D08C
		public void Update(float masterWeight)
		{
			if (!this.initiated)
			{
				return;
			}
			float num = this.weight * masterWeight;
			if (num <= 0f)
			{
				return;
			}
			this.solver.target = this.target;
			if (this.target != null)
			{
				this.IKPosition = this.target.position;
				this.IKRotation = this.target.rotation;
			}
			if (this.bone3 != null)
			{
				if (num >= 1f)
				{
					this.bone3.rotation = this.IKRotation * this.bone3RelativeToTarget;
				}
				else
				{
					this.bone3.rotation = Quaternion.Lerp(this.bone3.rotation, this.IKRotation * this.bone3RelativeToTarget, num);
				}
			}
			this.solver.IKPositionWeight = num;
			this.solver.Update();
		}

		// Token: 0x0400009A RID: 154
		[Tooltip("Master Weight for the finger.")]
		[Range(0f, 1f)]
		public float weight = 1f;

		// Token: 0x0400009B RID: 155
		[Tooltip("The first bone of the finger.")]
		public Transform bone1;

		// Token: 0x0400009C RID: 156
		[Tooltip("The second bone of the finger.")]
		public Transform bone2;

		// Token: 0x0400009D RID: 157
		[Tooltip("The (optional) third bone of the finger. This can be ignored for thumbs.")]
		public Transform bone3;

		// Token: 0x0400009E RID: 158
		[Tooltip("The fingertip object. If your character doesn't have tip bones, you can create an empty GameObject and parent it to the last bone in the finger. Place it to the tip of the finger.")]
		public Transform tip;

		// Token: 0x0400009F RID: 159
		[Tooltip("The IK target (optional, can use IKPosition and IKRotation directly).")]
		public Transform target;

		// Token: 0x040000A0 RID: 160
		public IKSolverLimb solver;

		// Token: 0x040000A1 RID: 161
		public Quaternion bone3RelativeToTarget;

		// Token: 0x040000A2 RID: 162
		public Vector3 bone3DefaultLocalPosition;

		// Token: 0x040000A3 RID: 163
		public Quaternion bone3DefaultLocalRotation;
	}
}
